from editor_base import EditorBase

from ..bases import widgets


class GroupEditor(EditorBase):
    topwidget_label = 'Group Properties'
    def init_editor(self, **kwargs):
        self.group = kwargs.get('group')
        vbox = widgets.VBox()
        
        hbox1 = widgets.HBox()
        self.txtName = widgets.Entry(label='Name', Property=(self.group, 'name'))
        hbox1.pack_start(self.txtName.topwidget)
        self.sbGroupIndex = widgets.SpinBtn(label='Group Number', Property=(self.group, 'Index'))
        hbox1.pack_start(self.sbGroupIndex.topwidget)
        vbox.pack_start(hbox1)
        
        hbox2 = widgets.HBox()
        names = ['id', 'Name', 'Num', 'Member', 'cell_active']
        types = [str, str, int, bool, bool]
        order = [2, 3, 1]
        editable = [3]
        l_kwargs = dict(name='Devices', column_names=names, list_types=types, 
                        column_order=order, columns_editable=editable, default_sort_column=2)
        self.device_list = widgets.TreeList(**l_kwargs)
        self.update_device_list()
        self.device_list.connect('cell_toggled', self.on_device_list_cell_toggled)
        hbox2.pack_start(self.device_list.topwidget, expand=True)
        
        l_kwargs.update(dict(name='Groups', column_names=['id', 'Name', 'Num', 'Member', 'cell_active'], 
                             list_types=[str, str, int, bool, bool], column_order=[1, 2, 3], 
                             columns_editable=[2]))
        self.group_list = widgets.TreeList(**l_kwargs)
        self.update_group_list()
        self.group_list.connect('cell_toggled', self.on_group_list_cell_toggled)
        hbox2.pack_start(self.group_list.topwidget, expand=True)
        vbox.pack_start(hbox2, expand=True)
        
        self.topwidget.pack_start(vbox, expand=True)
        
        self.group.bind(member_update=self.on_group_member_update, 
                        property_changed=self.on_group_property_changed)
        self.DeviceSystem.bind(new_device=self.on_ds_new_device)
        self.MainController.bind(new_group=self.on_mc_new_group)
        
    def unlink(self):
        self.group.unbind(self.on_group_member_update, self.on_group_property_changed)
        self.DeviceSystem.unbind(self.on_ds_new_device)
        self.MainController.unbind(self.on_mc_new_group)
        self.txtName.unlink()
        self.sbGroupIndex.unlink()
        super(GroupEditor, self).unlink()
        
    def update_device_list(self, key=None):
        if key:
            devices = {key:self.DeviceSystem.PatchedDevices[key]}
        else:
            devices = self.DeviceSystem.PatchedDevices
        for key, val in devices.iteritems():
            self.device_list.update({key:[val.name, val.device_num, key in self.group.members, True]})
        
    def update_group_list(self, key=None):
        if key:
            groups = {key:self.MainController.Groups[key]}
        else:
            groups = self.MainController.Groups
        for key, val in groups.iteritems():
            if val != self.group and 'MainController_' not in key:
                i = val.Index
                if i is None:
                    i = -1
                self.group_list.update({key:[val.name, i, key in self.group.members, True]})
            
    def on_device_list_cell_toggled(self, **kwargs):
        key = kwargs.get('key')
        device = self.DeviceSystem.PatchedDevices[key]
        if kwargs.get('state'):
            self.group.add_devices(device)
        else:
            self.group.del_member(key)
        
    def on_group_list_cell_toggled(self, **kwargs):
        key = kwargs.get('key')
        group = self.MainController.Groups[key]
        if kwargs.get('state'):
            self.group.add_member(**{key:group})
        else:
            self.group.del_member(key)
            
    def on_group_member_update(self, **kwargs):
        self.update_group_list()
        self.update_device_list()
#        keys = [kwargs.get(key).id for key in ['new', 'deleted'] if key in kwargs]
#        for key in keys:
#            if kwargs.get(key).base_class == 'Group':
#                self.update_group_list(key)
#            else:
#                self.update_device_list(key)

    def on_group_property_changed(self, **kwargs):
        obj = kwargs.get('obj')
        self.update_group_list(obj.id)
        
    def on_ds_new_device(self, **kwargs):
        key = kwargs.get('id')
        if kwargs.get('base') == 'PatchedDevice':
            self.update_device_list(key)
            
    def on_mc_new_group(self, **kwargs):
        key = kwargs.get('id')
        self.update_group_list(key)
